package com.sweet.satellite;


import android.annotation.TargetApi;
import android.content.SharedPreferences;
import android.os.Build;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Config {
    private static final String KEY_CREATE_DATE = "key_create_date";
    private static final String KEY_FIRST_SOURCE = "key_first_source";
    private static final String KEY_IS_FIRST_LAUNCH = "key_is_first_launch";
    private static final String KEY_LAST_OPEN_TIME = "key_last_open_time";
    private static final String KEY_PREVIOUS_VERSION_CODE = "key_previous_version_code";
    private static final String KEY_PREVIOUS_VERSION_NAME = "key_previous_version_name";
    private static final ExecutorService POOL = Executors.newSingleThreadExecutor();
    private static final String SATELLIT_APP_CONFIG = "satellit_app_config";
    private static SharedPreferences.Editor editor;
    private static SharedPreferences sharedPreference;

    static {
        Config.sharedPreference = GlobalConfig.getAppContext().getSharedPreferences("satellit_app_config",
                0);
        Config.editor = Config.sharedPreference.edit();
    }


    public static long getCreateDate() {
        return Config.sharedPreference.getLong("key_create_date", 0);
    }

    public static String getFirstSource() {
        return Config.sharedPreference.getString("key_first_source", "");
    }

    public static boolean getIsFirstLaunch() {
        return Config.sharedPreference.getBoolean("key_is_first_launch", true);
    }

    public static long getLastOpenTime() {
        return Config.sharedPreference.getLong("key_last_open_time", 0);
    }

    public static int getPreviousVersionCode() {
        return Config.sharedPreference.getInt("key_previous_version_code", 0);
    }

    public static String getPreviousVersionName() {
        return Config.sharedPreference.getString("key_previous_version_name", "");
    }

    public static void setCreateDate(long createDate) {
        Config.editor.putLong("key_create_date", createDate);
        Config.submit();
    }

    public static void setFirstSource(String firstSource) {
        Config.editor.putString("key_first_source", firstSource);
        Config.submit();
    }

    public static void setIsFirstLaunch(boolean isFirstLaunch) {
        Config.editor.putBoolean("key_is_first_launch", isFirstLaunch);
        Config.submit();
    }

    public static void setLastOpenTime(long lastOpenTime) {
        Config.editor.putLong("key_last_open_time", lastOpenTime);
        Config.submit();
    }

    public static void setPreviousVersionCode(int versionCode) {
        Config.editor.putInt("key_previous_version_code", versionCode);
        Config.submit();
    }

    public static void setPreviousVersionName(String versionName) {
        Config.editor.putString("key_previous_version_name", versionName);
        Config.submit();
    }

    @TargetApi(value = 9)
    private static void submit() {
        if (Build.VERSION.SDK_INT > 9) {
            Config.editor.apply();
        } else {
            Config.POOL.execute(new Runnable() {
                public void run() {
                    Config.editor.commit();
                }
            });
        }
    }
}

